<?php
/**
 * @desc    京东售后数据转换
 * @author: jintao
 * @since: 2016/7/20
 */
class erpapi_shop_matrix_360buy_response_aftersalev2 extends erpapi_shop_response_aftersalev2
{
    protected function _formatAddParams($params)
    {
        $sdf = parent::_formatAddParams($params);
        if ($sdf['refund_type'] == 'refund') {//新增京东售前退款格式转化
            //0、未审核 1、审核通过2、审核不通过 3、京东财务审核通过 4、京东财务审核不通过 5、人工审核通过 6、拦截并退款 7、青龙拦截成功 8、青龙拦截失败 9、强制关单并退款
            $status = $sdf['status'];
            if (in_array( $status,array('0')) ) {
                $sdf['status'] = 'WAIT_SELLER_AGREE';
                $sdf['refund_type'] = 'apply';
            }else if( in_array($status,array('1','5')) ){//申请通过
                $sdf['status'] = 'WAIT_BUYER_RETURN_GOODS';
                $sdf['refund_type'] = 'apply';
            }else if( in_array($status,array('2','4')) ){ //拒绝
                $sdf['status'] = 'SELLER_REFUSE_BUYER';
                $sdf['refund_type'] = 'apply';
            }else if( in_array($status,array('3','6','9')) ){ //成功
                $sdf['status'] = 'SUCCESS';
            }
            if ($sdf['order']['pay_bn'] == 'deposit' && $sdf['refund_fee']<=0){
                $sdf['refund_fee'] = $sdf['order']['payed'];
            }
        } else {
            $item = current($sdf['refund_item_list']['return_item']);
            $order_bn = $item['oid'];
            $sdf['order_bn'] = $order_bn;
            $this->__apilog['title'] = '店铺(' . $this->__channelObj->channel['name'] . ')售后业务处理[订单：' . $order_bn . ']';
            $this->__apilog['original_bn'] = $order_bn;
        }
        return $sdf;
    }

    protected function _getAddType($sdf) {
        if(in_array($sdf['refund_type'],array('refund','apply'))) { #退款
            return 'refund';
        } else { #退货
            return 'returnProduct';
        }
    }

    protected function _formatAddItemList($sdf, $convert = array()) {
        $itemList = $sdf['refund_item_list']['return_item'];
        $arrItem = array();
        if($itemList) {
            foreach ($itemList as $item) {
                $item['bn'] = $item['sku_id'];
                if($arrItem[$item['bn']]) {
                    $arrItem[$item['bn']]['num'] += $item['num'];
                } else {
                    $arrItem[$item['bn']] = $item;
                }
            }
        }
        return $arrItem;
    }
}